package com.byq.mm.gateway.adapter;

import java.util.LinkedList;
import java.util.List;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.byq.mm.gateway.AndroidHomeApplication;
import com.byq.mm.gateway.AssistanceApplication;
import com.byq.mm.gateway.R;
import com.tc.gateway.device.GeneralDevice;
import com.tc.gateway.device.Group;
import com.tc.gateway.device.Scene;
import com.tc.gateway.device.type.OnoffDevice;
import com.tc.gateway.device.type.OnoffLevelDevice;
import com.tc.gateway.device.listener.OnoffLevelListener;
import com.tc.gateway.device.listener.OnoffListener;

class Holder{
	OnoffDevice device;
	ImageView view;
}
public abstract class HomeAdapter extends BaseAdapter {
	protected AssistanceApplication application;
	protected Activity ctx;
	protected List devices;
	Handler myHandler = new Handler() { 
        public void handleMessage(Message msg) { 
              Holder d = (Holder)msg.obj;
              boolean onoff = d.device.getOnOff();
              String dct = d.device.getType();
              Drawable dw = null;
              if(onoff)
            	  dw = IconManager.getIconDrawable(ctx, dct);
              else
            	  dw = IconManager.getGrayIconDrawable(ctx, dct);
             d.view.setImageDrawable(dw);
        } 
   }; 

	public void reset(){
		if(application == null){
			devices = new LinkedList();
			return;
		}
		AndroidHomeApplication ha = (AndroidHomeApplication)application.getHomeApplication();
		if(ha == null){
			devices = new LinkedList();
			return;
		}
		if(!ha.isStarted()){
			devices = new LinkedList();
			return;
		}
		if(ha.getGatewayEngine() == null){
			devices = new LinkedList();
			return;
		}
		fetch();
		this.notifyDataSetChanged();
	}
    abstract protected void fetch() ;
	public HomeAdapter(AssistanceApplication es,Activity c) {
    	this.ctx = c;
    	application = es;
    	reset();

    }

    public View getView(final int position, View convertView, ViewGroup parent) {
    	LayoutInflater mInflater = LayoutInflater.from(ctx);
      
    	final ImageView i;
    	 TextView t;
        if (convertView == null) {
        	 convertView = mInflater.inflate(R.layout.grid_item_icon_text, null);

        	 i = (ImageView) convertView.findViewById(R.id.icon);
        	 t = (TextView) convertView.findViewById(R.id.text);
        	

            i.setScaleType(ImageView.ScaleType.FIT_CENTER);
           // i.setLayoutParams(new GridView.LayoutParams(50, 50));
            
        } else {
            i = (ImageView) convertView.findViewById(R.id.icon);
            t = (TextView) convertView.findViewById(R.id.text);
        }
        boolean gray = false;
        Object o = devices.get(position);
        String dct = null;
        if(o instanceof ManageAction){
        	ManageAction e = (ManageAction)o;
             dct = e.id;
         }
        if(o instanceof GeneralDevice){
        	GeneralDevice e = (GeneralDevice)o;
        	
         dct = e.getType();
         if(o instanceof OnoffDevice){
        	 gray = !((OnoffDevice)o).getOnOff();
         }
        }
        if(o instanceof Scene){
        	
             dct = "scene";
            }
        if(o instanceof Group){
        	
            dct = "group";
           }
        Drawable d;
        
        if(Character.isDigit(dct.charAt(0)))
        	dct = "d" + dct;
        if(!gray)
         d = IconManager.getIconDrawable(ctx, dct);
        else
        	d = IconManager.getGrayIconDrawable(ctx, dct);
        i.setImageDrawable(d);
        
        if(o instanceof GeneralDevice){
        	GeneralDevice e = (GeneralDevice)o;
        	if(e instanceof OnoffLevelDevice){
        		final String dtype = dct;
        		final OnoffLevelDevice de = (OnoffLevelDevice)e;
        		de.addListener(new OnoffLevelListener(){

					@Override
					public void onChange(boolean value) {
						Holder h = new Holder();
						h.device = de;
						h.view = i;
						Message m = new Message();
						m.obj = h;
						
						myHandler.sendMessage(m);
						AssistanceApplication app = (AssistanceApplication) application;
						app.getSound().playSound(0);
						
					}

					@Override
					public void onLevel(double level) {
						// TODO Auto-generated method stub
						
					}

					@Override
					public void range(double min, double max) {
						// TODO Auto-generated method stub
						
					}});
        	}else if(e instanceof OnoffDevice){
        		final String dtype = dct;
        		final OnoffDevice<OnoffListener> de = (OnoffDevice<OnoffListener>)e;
        		de.addListener(new OnoffListener(){

					@Override
					public void onChange(boolean value) {
						Holder h = new Holder();
						h.device = de;
						h.view = i;
						Message m = new Message();
						m.obj = h;
						
						myHandler.sendMessage(m);
						AssistanceApplication app = (AssistanceApplication) application;
						app.getSound().playSound(0);
					}});
        	}
        }
        String name = IconManager.getString(ctx, dct);
        t.setText(name);
        convertView.setOnTouchListener(new View.OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				return action(position);
			}
		});
        //i.set
//        i.setOnClickListener(new View.OnClickListener() {
//			
//			@Override
//			public void onClick(View v) {
//				Endpoint e = (Endpoint)HomeDeviceAdapter.this.getItem(position);
//				e.getDeviceId();
//			}
//		});

        return convertView;
    }
   abstract protected boolean action(final int position);
    public final int getCount() {
        return devices.size();
    }

    public final Object getItem(int position) {
        return devices.get(position);
    }

    public final long getItemId(int position) {
        return position;
    }
}